package com.wuliu.business.car;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.wuliu.model.car.Car;
import com.wuliu.util.DbPool;

public class CarObj {
	public static Car getCarById(String id){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Car car = new Car();
		if(id==null || id.equals(""))
			return car;
		String strsql = "SELECT * FROM WL_CAR WHERE CAR_ID=?";
		try{
			conn = DbPool.getConnection();
			pstmt = conn.prepareStatement(strsql);
			pstmt.setString(1,id);
			rs = pstmt.executeQuery();
			if(rs.next()){
				car.setCarId(rs.getString("CAR_ID"));
				car.setCarCode(rs.getString("CAR_CODE"));
				car.setWorkCode(rs.getString("WORK_CODE"));
				car.setCarType(rs.getString("CAR_TYPE"));
				car.setCarSize(rs.getBigDecimal("CAR_SIZE"));
				car.setCarTon(rs.getBigDecimal("CAR_TON"));
				car.setAddBy(rs.getString("ADD_BY"));
				car.setBuyDate(rs.getString("BUY_DATE"));
				car.setAddDate(rs.getString("ADD_DATE"));
				car.setAddTime(rs.getString("ADD_TIME"));
				car.setUpdBy(rs.getString("UPD_BY"));
				car.setUpdDate(rs.getString("UPD_DATE"));
				car.setUpdTime(rs.getString("UPD_TIME"));
				car.setActiveFlag(rs.getString("ACTIVE_FLAG"));
			}
		}catch(SQLException sqlE){
			sqlE.printStackTrace();
		}finally{
			DbPool.close(conn, pstmt, rs);
		}
		return car;
	}
	
	public static Car getCarByCode(String code){
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Car car = new Car();
		if(code==null || code.equals(""))
			return car;
		String strsql = "SELECT * FROM WL_CAR WHERE CAR_CODE=?";
		try{
			conn = DbPool.getConnection();
			pstmt = conn.prepareStatement(strsql);
			pstmt.setString(1,code);
			rs = pstmt.executeQuery();
			if(rs.next()){
				car.setCarId(rs.getString("CAR_ID"));
				car.setCarCode(rs.getString("CAR_CODE"));
				car.setWorkCode(rs.getString("WORK_CODE"));
				car.setCarType(rs.getString("CAR_TYPE"));
				car.setCarSize(rs.getBigDecimal("CAR_SIZE"));
				car.setCarTon(rs.getBigDecimal("CAR_TON"));
				car.setAddBy(rs.getString("ADD_BY"));
				car.setBuyDate(rs.getString("BUY_DATE"));
				car.setAddDate(rs.getString("ADD_DATE"));
				car.setAddTime(rs.getString("ADD_TIME"));
				car.setUpdBy(rs.getString("UPD_BY"));
				car.setUpdDate(rs.getString("UPD_DATE"));
				car.setUpdTime(rs.getString("UPD_TIME"));
				car.setActiveFlag(rs.getString("ACTIVE_FLAG"));
			}
		}catch(SQLException sqlE){
			sqlE.printStackTrace();
		}finally{
			DbPool.close(conn, pstmt, rs);
		}
		return car;
	}
}
